package org.example.sec12;

import com.google.common.util.concurrent.Uninterruptibles;
import com.seaway.models.sec12.*;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;
import org.example.sec12.respository.AccountRespository;

import java.util.concurrent.TimeUnit;

public class BankService extends BankServiceGrpc.BankServiceImplBase {

    @Override
    public void getAccountBalance(BalanceCheckRequest request, StreamObserver<AccountBalance> responseObserver) {
        sendAccountBalance(request, responseObserver);

    }

    private void sendAccountBalance(BalanceCheckRequest request, StreamObserver<AccountBalance> responseObserver) {
        var accountNumber = request.getAccountNumber();
        var balance = AccountRespository.getBalance(accountNumber);
        var accountBalance = AccountBalance.newBuilder().setAccountNumber(accountNumber).setBalance(balance).build();
        responseObserver.onNext(accountBalance);
        responseObserver.onCompleted();
    }

    @Override
    public void withDraw(WithDrawRequest request, StreamObserver<Money> responseObserver) {
        //校验
        sendMoney(request, responseObserver);
    }

    private void sendMoney(WithDrawRequest request, StreamObserver<Money> responseObserver) {
        try {
            int accountNumber = request.getAccountNumber();
            int amount = request.getAmount();
            for (int i = 0; i < (amount / 10); i++) {
                if (i == 3) {
                    throw new RuntimeException("oops");
                }
                var money = Money.newBuilder().setAmount(10).build();
                responseObserver.onNext(money);
                AccountRespository.deductAccount(accountNumber, 10);
                Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
            }
            responseObserver.onCompleted();
        } catch (Exception e) {
            responseObserver.onError(Status.INTERNAL.withDescription(e.getMessage()).getCause());
        }
    }

}
